var selectNode;
var parentNode;
var rootNode;
var categorytree = new eiTreeModel('ZMCD01');
var isAddedRefresh = false;
var treeObj;
//启动事件处理
$(document).ready(function() {
	efregion.show('ef_region_inqu');
	$("#_left").resizable({
		handles: "e",
		 iframeFix: true,  
		 stop: function( event, ui ) {
			 $("#_right").css("margin-left",  $("#_left").outerWidth());
		 }
	});
});
efform_onload = function ()
{
	$('#ef_region_inqu1').hide();
};
function reloadCurrentNode()
{ 
	if(selectNode!=null)selectNode.reload(); 
}
function configTree(tree) {
	  tree.emptyNodeAsLeaf = false;
	  tree.activeEmptyJudger = false;
	  tree.topNodeActive = false;
	  tree.nodeInitializer = treeNodeInitializer;
	  tree.initialExpandDepth = 1;
      tree.hideRoot = true;
      treeObj = tree;
      tree._rootNode.active(true);
} 
function treeNodeInitializer(node) {
	if ( node.top() )
	{ 
		rootNode=node;
	    node.active(true);
	    node.textClicked = function(){ };
	    return;
	} 
	
    node.textClicked = function(){ 
    	selectNode = node;
    	parentNode=node.parent(); 
		var str= new Array();   
		str=node.data().label.split("-");  
        var args="fdDirId="+str[0];
        if(selectNode.data().limit==null || selectNode.data().limit!="-1")
        {
            ef.get("_right_iframe").src="DispatchAction.do?efFormEname=ZMCD02&"+args; 
        	$('#_right_iframe').show();
        	$('#ef_region_inqu1').hide();
        }  
    	$('#ef_region_inqu1').hide();
    };
}
function efgrid_onAjaxSubmitSuccess(gridId, service_name,method_name, eiInfo){
    var grid = efgrid.getGridObject(gridId);
    grid.refresh(eiInfo);
    if(isAddedRefresh&&selectNode){
        if(selectNode.leaf){
            if(selectNode._parent){
                selectNode._parent.reload();
            }else{
                treeObj._rootNode.reload();
            }
        }else{
            selectNode.reload();
        }
        isAddedRefresh = false;
    }
}
button_update_onclick = function (){ 
	if(selectNode!=null)
	{
		if(selectNode.data().limit!=null && selectNode.data().limit=="-1")
		{
			EFAlert("根目录不能修改！");
		}
		else if(selectNode.data().label.indexOf("公用目录")>=0||selectNode.data().label.indexOf("指定目录")>=0)
		{
			EFAlert("只能修改个人目录或者部门目录！");
		}else
		{ 
			var str= new Array();   
			str=selectNode.data().label.split("-");   
			//查询该节点的数据
			var info=new EiInfo();
			info.set("fdDirId",str[0]);
			EiCommunicator.send("ZMCD01", "queryDirInfo", info, query_ajax_callback);
		}
	}else
	{
		EFAlert("请选择目录");
	}
}
var query_ajax_callback= {
	onSuccess: function(eiInfo) {  
		var block = eiInfo.getBlock("result");  
		if(block.getRows()[0]!=null)
		{
			$('#inqu_status-0-fdDepartmentId').val(block.getCell(0, "fdDepartmentId")); 
			$('#inqu_status-0-fdCreatorId').val(block.getCell(0, "fdCreatorId")); 
			$('#inqu_status-0-fdSuperDirId').val(block.getCell(0, "fdSuperDirId")); 
			$('#inqu_status-0-fdDirType').val(block.getCell(0, "fdDirType")); 
			$('#inqu_status-0-fdKeyId').val(block.getCell(0, "fdKeyId")); 
			$('#inqu_status-0-fdDirName').val(block.getCell(0, "fdDirName")); 
			$('#inqu_status-0-fdSerial').val(block.getCell(0, "fdSerial")); 
			$('#_right_iframe').hide();
			$('#ef_region_inqu1').show();
			efbutton.setButtonStatus("update", true);
		}
	},
	onFail:function(eMsg)
	{
		EFAlert("操作失败，原因："+eMsg); 
	}
}
button_insert_onclick = function (){ 
	if(selectNode!=null)
	{
		if(selectNode.data().label.indexOf("公用目录")>=0||selectNode.data().label.indexOf("指定目录")>=0)
		{
			EFAlert("只能添加个人目录或者部门目录！");
		}else
		{ 
			var str= new Array();   
			str=selectNode.data().label.split("-");   
		    $('#ef_region_inqu1').find("input,select,textarea").each(function (){$(this).val('');})
			$('#inqu_status-0-fdDepartmentId').val($('#fdCreateDepartmentId').val());
			$('#inqu_status-0-fdCreatorId').val($('#fdCreatorId').val());
			$('#inqu_status-0-fdMiningId').val($('#fdMiningId').val());
			$('#inqu_status-0-fdSuperDirId').val(str[0]);
			$('#inqu_status-0-fdDirType').val(str[1]);
			//给默认字段赋值
			$('#_right_iframe').hide();
			$('#ef_region_inqu1').show();
			efbutton.setButtonStatus("insert", true);
		}
	}else
	{
		EFAlert("请选择目录");
	}
}
button_delete_onclick = function (){ 
	if(selectNode!=null)
	{
		if(selectNode.data().limit!=null && selectNode.data().limit=="-1")
		{
			EFAlert("根目录不能删除！");
		}
		else if(selectNode.data().label.indexOf("公用目录")>=0||selectNode.data().label.indexOf("指定目录")>=0)
		{
			EFAlert("只能删除个人目录或者部门目录！");
		}
		else if(selectNode.data().creator!=$('#fdCreatorId').val())
		{
			EFAlert("不能删除，只有该目录的创建人可以删除此目录！");
		}
		else
		{ 
			var str= new Array();   
			str=selectNode.data().label.split("-");    
			var info = new EiInfo();
			//alert(str[0]);
			info.set("fdDirId", str[0]);
			EFConfirm('确定删除所选择目录?', '确认框', function(r) {
				EiCommunicator.send("ZMCD01", "canDelete", info, save_ajax_callback);
			});
		}
	}else
	{
		EFAlert("请选择目录");
	}
}
button_confirm_onclick = function (){ 
	if(efvalidateDiv("ef_region_inqu1")){ 
		var info = getDetail("ef_region_inqu1","inqu_status");
		if (null != info) {
			if($('#ef_region_inqu1').find('#inqu_status-0-fdKeyId').val())
			{
				EiCommunicator.send("ZMCD11", "update", info, save_ajax_callback);
			}else
			{
				EiCommunicator.send("ZMCD11", "insert", info, save_ajax_callback);
			}
		} 
	}
}
var save_ajax_callback= {
	onSuccess: function(eiInfo) { 
		if(selectNode!=null)
		{
			selectNode.reload(); 
			//selectNode._parent.reload();
		}
	},
	onFail:function(eMsg)
	{
		EFAlert("操作失败，原因："+eMsg); 
	}
}